<script setup>
import { onMounted, nextTick, ref, reactive, watch } from 'vue'
import { subSetting } from './system'
import { ElMessage } from 'element-plus'
import axios from 'axios';
const url = import.meta.env.VITE_backurl

const maxAge = ref(24)

onMounted(() => {

})

const onSubmit = () => {
  // maxAge.value转换为秒
  let maxAgeValue = maxAge.value * 60 * 60
  subSetting({ maxAge: maxAgeValue }).then(res => {
    if (res.data.code === 200) {
      ElMessage.success('设置成功')
    } else {
      ElMessage.error('设置失败')
    }
  })
}

const getNextDay = (date) => {
  let arr = date.split('-')
  let year = Number(arr[0])
  let month = Number(arr[1])
  let day = Number(arr[2])
  let nextDay = new Date(year, month - 1, day + 1)
  let nextYear = nextDay.getFullYear()
  let nextMonth = nextDay.getMonth() + 1
  let nextDate = nextDay.getDate()
  nextMonth = nextMonth < 10 ? '0' + nextMonth : nextMonth
  nextDate = nextDate < 10 ? '0' + nextDate : nextDate
  return nextYear + '-' + nextMonth + '-' + nextDate
}

const dateValue = ref(new Date().toLocaleDateString().split('/').map(item => {
  return item.length < 2 ? '0' + item : item
}).join('-')),
  fileName = ref('current_hl_full.png'),
  finalName = ref('current')

const pullPng = async () => {

  let startTime = '2023-07-05'
  while (getNextDay(startTime) < '2023-08-02') {
    startTime = getNextDay(startTime)
    for (let i = 0; i < 24; i++) {
      let time = startTime.split('-').join('') + (i < 10 ? '0' + i : i)
      await axios({
        method: 'get',
        url: 'http://www.oceanread.com/output/' + time + '/' + fileName.value,
        responseType: 'blob'
      }).then(res => {
        let formData = new FormData()
        formData.append('file', res.data)

        axios({
          method: 'post',
          url: url + '/up/savePng',
          params: {
            type: finalName.value,
            time: startTime.split('-').join(''),
            fileName: finalName.value + '.' + (i < 10 ? '0' + i : i) + '.png'
          },
          data: formData,
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }).then(res => {
          console.log(res);
        })
      })
    }
  }


}
</script>

<template>
  <div class="Setting">
    <div class="card_hook">
      <span>登录过期时间：</span>
      <el-input-number v-model="maxAge" :min="0" />
      <span>小时</span>
      <br />
      <el-button type="primary" size="default" @click="onSubmit">提交</el-button>
      <!-- 测试一下东西 -->
      <!-- <div>
        <el-date-picker v-model="dateValue" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
          placeholder="选择日期" />
      </div>
      <el-input v-model="fileName" placeholder="" size="" clearable></el-input>
      <el-input v-model="finalName" placeholder="" size="" clearable></el-input>

      <el-button type="primary" size="default" @click="pullPng">开始</el-button> -->

    </div>


  </div>
</template>

<style lang='less' scoped>
.Setting {
  width: 100%;
  height: 100%;
  padding: 15px;

  .card_hook {
    height: 100%;
    padding: 10px;
  }
}
</style>